import { switchTab, navigateTo } from '../utils/aysncWx'
let app = getApp();
Component({
  options: {
    styleIsolation: 'apply-shared'
  },
  lifetimes: {
     ready() {

      this.setGlobalData();
      
      this.animation =  wx.createAnimation({
        duration: 100
      })
      this.angle = 0;
     
    },

  },
  data: {
    selected: 0,
    publishChecked: false,
    list: [
      {
        pagePath: '/pages/home/home',
        text: '首页',
        icon: 'cuIcon-home',
        selectIcon: 'cuIcon-homefill'
      },
      {
        pagePath: '/pages/shopping/shopping',
        text: '购物',
        icon: 'cuIcon-shop',
        selectIcon: 'cuIcon-shopfill'
      },
      {
        pagePath: '/pages/shopping/shopping',
        text: '发布',
        icon: 'cuIcon-shop',
        selectIcon: 'cuIcon-shopfill'
      },
      {
        pagePath: '/pages/message/message',
        text: '消息',
        icon: 'cuIcon-message',
        selectIcon: 'cuIcon-messagefill'
      },
      {
        pagePath: '/pages/mine/mine',
        text: '我的',
        icon: 'cuIcon-my',
        selectIcon: 'cuIcon-myfill'
      }
    ],
    publishItem: [
      {

        name: '寻物启事',
        iconClass: 'iconfont icon-xunwuqishi',
        pagePath: '/pages/lostGoods/lostGoods'

      },
      {
        name: '失物招领',
        iconClass: 'iconfont icon-shiwuzhaoling2',
        pagePath: '/pages/findGoods/findGoods'
      },

      {
        name: '答疑解惑',
        iconClass: 'iconfont icon-dayijiehuo1',
        pagePath: '/pages/answerQuestion/answerQuestion'
      },
      {
        name: '畅所欲言',
        iconClass: 'iconfont icon-changsuoyuyan',
        pagePath: '/pages/talkAny/talkAny'
      },
      {
        name: '卖东西',
        iconClass: 'iconfont icon-mai',
        pagePath: '/pages/saling/saling'
      }

    ]
  },
  methods: {
    async switchTab(e) {
      const data = e.currentTarget.dataset
      if (data.index != 2) {
        const url = this.data.list[data.index].pagePath
        this.canclePublishItem();
        // this.setData({
        //   selected: data.index
        // })
        await switchTab({ url })
      } else {
        let publishChecked = !this.data.publishChecked
        this.setData({
          publishChecked
        })

        this.rotate()
      }

    },
    
    async setGlobalData(){
      if(app.globalData.tabbarHeight === 0){
        const res = await this.getTabbar('#tabbar');
        let data = app.globalData;
        app.globalData.tabbarHeight = res.height;
        app.globalData.container = data.screenHeight - data.CustomBar - res.height;
      }

    },

    getTabbar(selector){

      return new Promise((resolve) => {
        let selQuery = wx.createSelectorQuery().in(this)       
        selQuery.select(selector).boundingClientRect(res => {
            
            resolve(res)
        }).exec();
      })
    },

    rotate() {
      let rotateAngle = 0;
      this.data.publishChecked ? rotateAngle = 135 : rotateAngle = 360 - 135
      this.angle += rotateAngle;
      this.animation.rotate(this.angle).step()
      this.setData({ animation: this.animation.export() })
    },
    async handlePublishItem(e) {
      const { index } = e.currentTarget.dataset;
      this.canclePublishItem()
      await navigateTo({
        url: this.data.publishItem[index].pagePath
      })


    },
    canclePublishItem() {
      if (this.data.publishChecked) {
        this.rotate()
      }
      this.setData({
        publishChecked: false
      })

    },
  }
})